import { Order } from 'order-model.js';
var order = new Order();
Page({
  data: {
    statusType: ["全部", "待付款", "待核销","已完成"],
    currentTpye: 0,
    page: 1,
    hiddenNomore: true,
    hiddenLoading: true,
    hiddenNullData: true,
    onLoading: false,
    nomore: false,
    status:'',
    orderArr: []
  },
  onLoad: function (options) {
    var curType = options.type ? options.type : 0;
    this.setData({
      currentTpye : curType
    })
    
    this._getData();
  },
  statusTap: function (e) {
    var that = this;
    var curType = e.currentTarget.dataset.index;
    //重置数据
    this.setData({
      currentTpye: curType,
      orderArr: [],
      hiddenNullData: true,
      hiddenNomore: true
    });
    this.data.page = 1;
    this.data.nomore = false;
    if (curType == 0) {
      this.data.status = '';
    }else if (curType == 1){
      this.data.status = 1;
    }else if (curType == 2) {
      this.data.status = 3;
    }else if (curType == 3) {
      this.data.status = 5;
    }
    this._getData();
  },
  _getData: function () {
    var that = this;
    var args = {
      page: this.data.page,
      num: 15,
      status:this.data.status
    }
    that.setData({
      hiddenLoading: false
    })
    order.lists(args, (res) => {
      
      var items = res.items;
      var items = that.data.orderArr.concat(items);
      that.setData({
        orderArr:items
      })
      //隐藏正在加载中
      that.setData({
        hiddenLoading: true
      })
      if (items.length == 0) {
        //显示没有数据
        that.setData({
          hiddenNullData: false
        })
      }
      if (that.data.page >= res._meta.pageCount) {
        that.data.nomore = true;
        if (that.data.page > 1) {
          //显示没有更多
          that.setData({
            hiddenNomore: false
          })
        }
      }
    });
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.nomore) {
      return false;
    }
    this.data.page++;
    this._getData();
  },
  goTodetails: function (e) {
    var status = e.currentTarget.dataset.status;
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: 'details?id=' + id + '&status=' + status,
    })
  },
  goHome:function(){
    wx.switchTab({
      url: '/pages/home/home',
    })
  }
})